   //获取应用实例，请求数据的方法
const {
    request,
    PageContent
    } = require("../../utils/util.js");
var app = getApp();
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var user = require('../../services/user.js');
Page({
    data: {
        num: 1,                   //默认选中抢购的第一项
      products_data: [
        // { id: "orange", src: "/static/imgs/market/orange.jpg", description: "赣南脐橙/箱/10斤装", product_name: "橙子", now_price: "88", old_price: "108", sold: "100" }, 
        // { id: "apple", src: "/static/imgs/market/orange.jpg", description: "富平红富士/箱/10斤装富平红富士/箱/10斤装", product_name: "苹果", now_price: "128", old_price: "158", sold: "200" },
        // { id: "orange", src: "/static/imgs/market/orange.jpg", description: "赣南脐橙/箱/10斤装", product_name: "橙子", now_price: "88", old_price: "108", sold: "100" }, 
        // { id: "apple", src: "/static/imgs/market/orange.jpg", description: "富平红富士/箱/10斤装富平红富士/箱/10斤装", product_name: "苹果", now_price: "128", old_price: "158", sold: "200" }
      ],        //页面秒杀商品的信息
        is_hide_loadmore: true    //控制加载更多的显示
    },

    /**
     * 页面加载时
     */
    onLoad: function () {
        //保存this对象
        const _this = this;

      var url = api.findgoodlist; 
      var data = {bizType:2};
      wx.request({
          url:url,
          method:"POST", 
          data: data, 
          success: function (res) {
            console.log(res.data.responseBody.campaignList)
            var res_data = res.data.responseBody.campaignList
            _this.setData({
              products_data: res_data
            })
            app.globalData.queryGoodLists = res_data
          }
        });
    },

    /**
     * 初始化页面
     * @pram data--页面加载的数据
     */
    initPage: function (data) {
        this.setData({
            products_data: data
        });
    },

    /**
     * 点击抢购的时间切换样式
     * @pram e--当前点击对象的属性集合
     */
    clickNum: function (e) {
        if (e.target.dataset.num) {
            //设置当前点击的项为选中样式
            this.setData({
                num: e.target.dataset.num
            })
        }
    },

    /**
     * 点击searchbar进入搜索页面
     */
    goSearchPage: function () {
        // 跳转到search页面
        PageContent.loadNavigate("search/search");
    },

    /**
     * 点击商品进入商品对应的详情页
     * @pram e--当前点击对象的属性集合
     */
    goDetail: (e) => {
        // 带上商品id跳转商品详情页
        PageContent.loadNavigate(`cart/goods_detail/goods_detail?product_id=${e.currentTarget.dataset.id}`);
    },

    /**
     * 下拉加载更多
     */
    onReachBottom: function () {
        //保存this对象
        const _this = this;
        //页面已存在的商品数据
        let exsit_data = _this.data.products_data,
            products_data;
        //请求数据
        request({
            url: "GetSeckillProducts",
            loading: true,
            func: (data) => {
                //合并已有的数据和请求到加载更多的数据
                products_data = [...exsit_data, ...data["products_data"]];
                // 成功后加载页面
                _this.setData({
                    products_data: products_data,
                    is_hide_loadmore: false
                });
            }
        });
    },
    gotoCart: function (e){
      user.checkLogin().then(res => {
        user.getUserinfoByButton(e).then(res => {
          wx.navigateTo({
            url: 'cart/cart',
          })
        })
      }).catch(() => {
        //未登录跳转至登录页面
        wx.showModal({
          title: '提示',
          content: '请登录后进行报名',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定');
              user.getUserinfoByButton(e)
              wx.navigateTo({
                url: '/pages/auth/registerAndLogin/registerAndLogin',
              })
            } else {
              console.log('用户点击取消');
            }
          }
        })
      });
    },
    gotoMy_order: function (e) {
      user.checkLogin().then(res => {
        user.getUserinfoByButton(e).then(res => {
          wx.navigateTo({
            url: 'my_order/my_order',
          })
        })
      }).catch(() => {
        //未登录跳转至登录页面
        wx.showModal({
          title: '提示',
          content: '请登录后进行报名',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定');
              user.getUserinfoByButton(e)
              wx.navigateTo({
                url: '/pages/auth/registerAndLogin/registerAndLogin',
              })
            } else {
              console.log('用户点击取消');
            }
          }
        })
      });
    },
    gotoLogistics_list: function (e) {
      user.checkLogin().then(res => {
        user.getUserinfoByButton(e).then(res => {
          wx.navigateTo({
            url: 'logistics_list/logistics_list',
          })
        })
      }).catch(() => {
        //未登录跳转至登录页面
        wx.showModal({
          title: '提示',
          content: '请登录后进行报名',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定');
              user.getUserinfoByButton(e)
              wx.navigateTo({
                url: '/pages/auth/registerAndLogin/registerAndLogin',
              })
            } else {
              console.log('用户点击取消');
            }
          }
        })

      });
    }
});
